package com.my.swas.sso.module.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.Splitter;
import com.my.swas.common.base.ResponseResult;
import com.my.swas.common.utils.BeanCopierUtil;
import com.my.swas.sso.common.base.BaseController;
import com.my.swas.sso.common.shiro.ShiroUtils;
import com.my.swas.sso.module.admin.entity.Role;
import com.my.swas.sso.module.admin.entity.Auth;
import com.my.swas.sso.module.admin.dto.RoleDto;
import com.my.swas.sso.module.admin.service.AuthService;
import com.my.swas.sso.module.admin.service.RoleService;
import com.my.swas.sso.module.admin.vo.RoleTreeVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @author: :MaYong
 */
@Slf4j
@Controller
@Transactional
@RequestMapping("admin/role")
public class RoleController extends BaseController{
    @Resource
    RoleService roleService;

    @Resource
    AuthService roleResourcesService;

    @ApiOperation(value = "分页获取所有的角色信息", notes = "参数描述", code = 200, produces = "application/json")
    @PostMapping(value = "list")
    @ResponseBody
    public IPage getRoleList(Model model, RoleDto roleDto) {
        Role role = new Role();
        BeanCopierUtil.convert(roleDto, role);
        IPage pageInfo =  roleService.page(roleDto.getPage(), new QueryWrapper<>(role));
        return pageInfo;
    }

    /**
     * 组装用户所属角色数据
     *
     * @param userId
     * @param model
     * @return
     */
    @GetMapping(value = "allRoleByUserId")
    @ResponseBody
    public List<RoleTreeVo> getAllRole(@RequestParam("userId") Long userId, Model model) {
        return roleService.getRoleListByUserId(userId);
    }


    /**
     * 保存角色
     *
     * @param roleDto
     * @return
     */
    @PostMapping("save")
    @ResponseBody
    public ResponseResult saveRole(RoleDto roleDto) {
        Role role = new Role();
        BeanCopierUtil.convert(roleDto, role);
        roleService.saveOrUpdate(role);
        return ResponseResult.ok;
    }

    /**
     * 删除角色
     *
     * @param ids
     * @return
     */
    @GetMapping("remove")
    @ResponseBody
    public ResponseResult removeRole(String ids) {
        roleService.removeByIds(Splitter.on(",").splitToList(ids));
        return ResponseResult.ok;
    }

    /**
     * 跳转编辑角色页面
     *
     * @param id
     * @return
     */
    @GetMapping("toEditPage")
    public String toEditPage(Long id, Model model) {
        Role role = new Role();
        role.setId(id);
        Role dbRole = roleService.getOne(new QueryWrapper<>(role));
        model.addAttribute("role", dbRole);
        return "layui/system/role/edit";
    }


    @PostMapping("grantRes")
    @ResponseBody
    public ResponseResult grantRes(Long roleId, @RequestParam(value = "resIds[]") String[] resIds, Model model) {
        Auth resources = new Auth();
        resources.setRoleId(roleId);
        roleResourcesService.remove(new QueryWrapper<>(resources));

        if (resIds != null && resIds.length > 0) {
            for (String id : resIds) {
                Auth roleResources = new Auth();
                roleResources.setRoleId(roleId);
                roleResources.setMenuId(Long.valueOf(id));
                roleResourcesService.save(roleResources);
            }

            ShiroUtils.refresh();
            refreshMainTree();
            return ResponseResult.ok;
        } else {
            return ResponseResult.error;
        }
    }

}
